<template>
  <section class="add-article">
    <el-form
      :model="ValidateForm"
      ref="ruleForm"
      label-width="100px"
      :rules="rules"
    >
      <el-form-item label="文章标题：" prop="title">
        <el-input
          v-model="ValidateForm.title"
          autocomplete="off"
          placeholder="请输入文章标签"
        ></el-input>
      </el-form-item>
      <el-form-item label="文章内容：" prop="content">
        <quillEditor
          v-model="ValidateForm.content"
          ref="myQuillEditor"
          :options="editorOption"
          @blur="questiononblur"
        />
      </el-form-item>
      <el-form-item label="视频地址：" prop="url">
        <el-input
          v-model="ValidateForm.url"
          autocomplete="off"
          placeholder="请输入视频地址"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="end">
          <el-button @click="cancel('ruleForm')">取消</el-button>
          <el-button
            v-if="changeShow"
            type="primary"
            @click="submitForm('ruleForm')"
            >确认</el-button
          >
          <el-button v-else type="primary" @click="changeForm('ruleForm')"
            >修改</el-button
          >
        </el-row>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import { add, update } from '@/api/hmmm/articles'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  ['blockquote'], // 引用  代码块-----['blockquote', 'code-block']
  ['code-block', 'image', 'link'] // 链接、图片、视频-----['link', 'image', 'video']
]
export default {
  data () {
    return {
      ValidateForm: {
        title: this.title,
        content: this.content,
        url: this.url
      },
      rules: {
        title: [
          {
            required: true,
            message: '文章标题不能为空',
            trigger: 'blur'
          }
        ],
        content: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ]
      },
      editorOption: {
        //  富文本编辑器配置
        modules: {
          toolbar: toolbarOptions
        }
      }
    }
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    content: {
      type: String,
      default: ''
    },
    url: {
      type: String,
      default: ''
    },
    artId: {
      type: [Number, String]
    },
    changeShow: {
      type: Boolean,
      required: true
    }
  },
  components: { quillEditor },
  methods: {
    questiononblur () {
      this.$refs.ruleForm.validateField('content')
    },
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 表单验证成功,发送请求,添加文章
          //   console.log(this.ValidateForm.title, this.ValidateForm.content)
          try {
            await add({
              title: this.ValidateForm.title,
              articleBody: this.ValidateForm.content,
              videoURL: this.ValidateForm.url
            })
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$emit('close', false)
            this.$emit('updateArticle')
            this.$refs[formName].resetFields()
          } catch (err) {
            this.$message.error('操作失败')
          }
        } else {
          // 表单验证不通过，不做任何操作
          return false
        }
      })
    },
    cancel (formName) {
      // 取消关闭弹出层，同时也要重置表单的验证规则
      this.$emit('close', false)
      this.$refs[formName].resetFields()
    },
    changeForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            await update({
              id: this.artId,
              articleBody: this.ValidateForm.content,
              title: this.ValidateForm.title,
              videoURL: this.ValidateForm.url
            })
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$emit('close', false)
            this.$emit('updateArticle')
            this.$refs[formName].resetFields()
          } catch (err) {
            this.$message.error('操作失败')
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.add-article {
  ::v-deep .ql-editor {
    height: 200px !important;
  }
}
</style>
